<template>
	<view style="height: 100%;">
		<view class="top-layout">
			<input class="input-layout" placeholder="请输入优惠券码" @input="inputCode" :value="couponCode">
			<view class="input-txt" @click="exchange">兑换</view>
		</view>
		<view class="tabbar">
			<view class="tabbar-item" @click="chooseItem(0)" :class="{active : index == 0}">
				<view style="display: flex; padding-top: 10px; padding-bottom: 10px; justify-content: center;">可用</view>
				<view v-if="index == 0" class="line"></view>
			</view>
			<view class="tabbar-item" @click="chooseItem(1)" :class="{active : index == 1}">
				<view style="display: flex; padding-top: 10px; padding-bottom: 10px; justify-content: center;">不可用</view>
				<view v-if="index == 1" class="line"></view>
			</view>
		</view>
		<view v-for="item in couponList">
			<view class="coupon-layout">
				<view class="coupon-layout-top">
					<image class="coupon-layout-icon" src="/static/weapp/ic_redpacket.png" mode="aspectFit"></image>
					<view class="coupon-layout-center">
						<block v-if="index == 0">
							<view style="margin-left: 10px; margin-top: 5px; font-weight: bold;">{{item.name}}</view>
							<view style="margin-left: 10px; margin-bottom: 5px; margin-top: auto; color: red; font-size: 14px;">{{item.expireTime}}到期</view>
						</block>
						<block v-if="index == 1">
							<view style="margin-left: 10px; margin-top: 5px; color: gainsboro;">{{item.name}}</view>
							<view style="margin-left: 10px; margin-bottom: 5px; margin-top: auto; color: gainsboro; font-size: 14px;">{{item.expireTime}}到期</view>
						</block>
					</view>
					<view class="coupon-layout-right">
						<block v-if="index == 0">
							<view style="margin-top: 10px; color: red; font-size: 24px; text-align: center;">¥{{item.money}}</view>
							<view v-if="item.threshold != -1" style="margin-bottom: 5px; margin-top: auto; font-size: 14px;">满{{item.threshold}}可用</view>
							<view v-if="item.threshold == -1" style="margin-bottom: 5px; margin-top: auto; font-size: 14px;">无门槛</view>
						</block>
						<block v-if="index == 1">
							<view v-if="index == 1" style="margin-top: 10px; color: gainsboro; font-size: 24px; text-align: center;">¥{{item.money}}</view>
							<view v-if="item.threshold != -1" style="margin-bottom: 5px; margin-top: auto; font-size: 14px; color: gainsboro;">满{{item.threshold}}可用</view>
							<view v-if="item.threshold == -1" style="margin-bottom: 5px; margin-top: auto; font-size: 14px; color: gainsboro;">无门槛</view>
						</block>
					</view>
				</view>
				<view class="coupon-layout-bottom">
					{{item.desc}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { http } from '@/common/service/service.js'
	
	export default {
		data() {
			return {
				index: 0,
				couponCode: null,
				couponList: []
			}
		},
		methods: {
			
			chooseItem(item) {
				this.index = item
				this.couponList.splice(0);
				this.getCoupon(this.index == 0 ? false : true);
			},
			
			async getCoupon(isExpired) {
				var res = await new Promise((resolve, reject) => {
					http.get('/coupon/list?isExpired=' + isExpired).then(res => {
						resolve(res.data);
					})
				});
				if (res.code == 200) {
					var list = res.data;
					for (var index in list) {
						var item = list[index];
						this.couponList.push(item);
					}
				}
			},
			
			inputCode(e) {
				this.couponCode = e.detail.value
			},
			
			async exchange() {
				if (null == this.couponCode) {
					uni.showToast({
						icon: 'error',
						title: '请输入优惠券码'
					})
					return;
				}
				
				var res = await new Promise((resolve, reject) => {
					http.post('/coupon/bind?couponId=' + this.couponCode).then(res => {
						resolve(res.data);
					})
				});
				if (res.code == 200) {
					uni.showToast({
						icon: 'success',
						title: '兑换成功'
					})
					this.couponList.splice(0);
					this.getCoupon(this.index == 0 ? false : true);
				} else {
					uni.showToast({
						icon: 'error',
						title: res.msg
					})
				}
			}
		},
		
		onShow() {
			this.couponList.splice(0);
			this.getCoupon(this.index == 0 ? false : true);
		}
	}
</script>

<style>
	.top-layout {
		display: flex;
		flex-direction: row;
		background-color: white;
		padding-top: 10px;
		padding-bottom: 10px;
	}
	.input-layout {
		width: 70%;
		height: 30px;
		margin-left: 10px;
		background-color: rgb(240, 240, 240);
		border-radius: 20px;
		padding-left: 20px;
	}
	.input-txt {
		font-size: 20px;
		color: rgb(255, 85, 127);
		margin-top: auto;
		margin-bottom: auto;
		margin-left: auto;
		margin-right: 20px;
	}
	.tabbar {
		display: flex;
		flex-direction: row;
	}
	.tabbar-item {
		display: flex;
		flex-direction: column;
		width: 50%;
		text-align: center;
		color: rgb(100, 100, 100);
		background-color: white;
		justify-content: center;
	}
	.active {
		color: black;
	}
	.line {
		display: flex;
		width: 50px;
		height: 2px;
		background-color: red;
		text-align: center;
		justify-content: center;
		margin-left: auto;
		margin-right: auto;
	}
	.coupon-layout {
		display: flex;
		flex-direction: column;
		height: 100px;
		background-color: white;
		border-top-left-radius: 10px;
		border-top-right-radius: 10px;
		border-bottom-left-radius: 10px;
		border-bottom-right-radius: 10px;
		margin-top: 10px;
		margin-bottom: 10px;
		margin-left: 10px;
		margin-right: 10px;
	}
	.coupon-layout-top {
		display: flex;
		flex-direction: row;
		height: 70%;
		margin-left: 10px;
		margin-right: 10px;
		border-bottom: 1px dashed #969696;
	}
	.coupon-layout-icon {
		display: flex;
		width: 64px; 
		height: 64px;
		margin-top: auto;
		margin-bottom: auto;
	}
	.coupon-layout-center {
		display: flex;
		flex-direction: column;
		height: 64px;
		margin-top: auto;
		margin-bottom: auto;
	}
	.coupon-layout-right {
		display: flex;
		flex-direction: column;
		width: 100px;
		height: 64px;
		margin-top: auto;
		margin-bottom: auto;
		margin-left: auto;
		text-align: center;
	}
	.coupon-layout-bottom {
		display: flex;
		flex-direction: row;
		font-size: 14px;
		color: #969696;
		margin-left: 10px;
		margin-top: auto;
		margin-bottom: auto;
	}
</style>